#!/usr/bin/perl
#Version: 1.00b

use POSIX;
use File::Find::Rule;
use MIME::Lite;

$outputresults          =       '1';                          # Do you want to Output Results to STDOUT?
$emailresults           =       '1';                          # Do you want to Email the Results?
$admin_email            =       'support@r2hosting.net.au';   # Enter Your E-Mail Address

#####################################################################################
# These settings are required to auto-select the appropriate user_home              #
# directories from /etc/passwd, ignoring standard users.                            #
# On most unix systems hosting users start quit alot higher then system             #
# users and there maybe standard users in the higher than ranges (nornally 65000 >) #
#####################################################################################
$uid_from               =       '500';
$uid_to                 =       '55001';

#####################################################################################
# SHARED HOSTING USERS                                                              #
# If you are a sinlge installation, shared hosting user, the script will auto-      #
# select your own UiD from /etc/passwd (as long as the range you entered above      #
# actually includes your UiD.                                                       #
#####################################################################################


# Sets this Scripts Priority (20 lowest, -20 highest)
setpriority(PRIO_PROCESS,0,19);

my $location;
my $findfilename;
my $product;
my $ver;
my $devstat;
my $devlev;
my $build;
my $codename;
my $reldate;


if($outputresults == 1){ print "Starting Search, This will take a while..."; }

# This is the Joomla Mambo default metaData file type
$findfilename = '*.xml'; 

####################################################################################
# This is where we cruise through /etc/passwd to find the users home_directory     #
# We select a range of UiD's to avoid including non-hosting end-users.             #
####################################################################################

      open(passwd, '/etc/passwd');
      while (<passwd>) {
        ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
                = split(/:/);

      if(($uid > $uid_from) && ($uid < $uid_to)) {
        $searchpath = "$home";
      }
  
  print "$searchpath Complete\n";

my @files = File::Find::Rule->file
                            ->name("$findfilename")
                            ->in("$searchpath");


foreach $file (@files){
        undef $product;
        undef $ver;
        undef $devstat;
        undef $devlev;
        undef $build;
        undef $codename;
        undef $reldate;
        open(FM,"$file");
        while (<FM>) {

                if(m/\s*.*<mosinstall\stype=\"(\w*)\"/x) { $product = $1; }
                if(m/\s*.*<mosinstall\s*.*type=\"(\w*)\"/x) { $product = $1; }

                if(m/\s*.*<name>(.*)\s*<\/name>/x){ $ver = $1; } 
                if(m/\s*.*<title>(.*)\s*<\/title>/x){ $ver = $1; } 

                if(m/\s*.*<version>(.*)\s*<\/version>/x) { $devstat = $1; }

                if(m/\s*.*<creationDate>(.*)\s*<\/creationDate>/x) { $devlev = $1; }

                if(m/\s*.*<author>(.*)\s*<\/author>/x) { $build = $1; }

                if(m/\s*.*<authorUrl>(.*)\s*<\/authorUrl>/x) { $codename = $1; }

                if(m/\s*.*<description>(.*)\s*<\/description>/x) { $reldate = $1; last; }

        } # close while
        close(FM);


if ($product eq "") { } else {

               push @BADVERSIONS, "$file~$product~$ver~$devstat~$devlev~$build~$codename~$reldate";
}


} #close for



}


if($emailresults == 1){ sendmail(); }
if($outputresults == 1){ displayresults(); }

### SUBS ###
sub sendmail {
        $hostname = `hostname`;
        chomp($hostname);

#if ($product) {

        foreach (@BADVERSIONS){
                ($file,$cmsproduct,$cmsversion,$cmsdevstat,$cmsdevlev,$cmsbuild,$cmscodename,$cmsreldate) = split("~", $_, 8);

                if($cmsbuild eq "Joomla! Project") {
                  $cmscore = "CORE ";
                    } elsif ($cmsbuild eq "Mambo Project") {
                      $cmscore = "LEGACY ";
                    } else {
                      $cmscore = "CUSTOM ";
                }

                $cmsproduct =~ tr/a-z/A-Z/;
                $instances .= "    -----------------------------------------------------------------------------------\n";
                $instances .= "    $cmscore$cmsproduct: $cmsversion  Ver: $cmsdevstat  Date: $cmsdevlev\n";
                $instances .= "    Author: $cmsbuild ($cmscodename)\n";

                if($cmsreldate lt 60) {
                    $instances .= "    Description: $cmsreldate\n";
                      } else {
                        $shortdesc = substr($cmsreldate, 0, 60);
                        $instances .= "    Description: $shortdesc...\n";
                     }

                ($newFile = $file) =~ s/\/$findfilename//ig;
                $instances .= "    $newFile\n";
                $instances .= "    ===================================================================================\n\n";
        }


        $subject = "Joomla/Mambo Component & Module Version Check : R2A2\n";
        $msg = "\n\nThe following installations were found...\n\n\n";

        open(SENDMAIL,"|/usr/sbin/sendmail -t");
        print SENDMAIL "To: <$admin_email>\n";
        print SENDMAIL "From: root\@$hostname\n";
        print SENDMAIL "Subject: $subject\n\n";
        print SENDMAIL "$msg $instances";
        close(SENDMAIL);
}

sub displayresults {
        foreach (@BADVERSIONS){
                ($file,$cmsproduct,$cmsversion,$cmsdevstat,$cmsdevlev,$cmsbuild,$cmscodename,$cmsreldate) = split("~", $_, 8);

                if($cmsbuild eq "Joomla! Project") {
                  $cmscore = "CORE ";
                    } elsif ($cmsbuild eq "Mambo Project") {
                      $cmscore = "LEGACY ";
                    } else { 
                      $cmscore = "CUSTOM "; 
                }
                $cmsproduct =~ tr/a-z/A-Z/;
                print "\n";
                print "$cmscore$cmsproduct: $cmsversion  Ver: $cmsdevstat  Date: $cmsdevlev\n";
                print "Author: $cmsbuild  ($cmscodename)\n";

                if($cmsreldate lt 80) {
                    print "Description: $cmsreldate\n"; 
                      } else {
                        $shortdesc = substr($cmsreldate, 0, 80);
                        print "Description: $shortdesc...\n";
                     }
         
                ($newFile = $file) =~ s/\/$findfilename//ig;
                print "$newFile\n";
                print "--------------------------------------------------------------------------------------------------\n";
       }
}
